const app = getApp();
const token = require('token.js');

/**
 * 请求
 * @param url
 * @param param
 * @param succ
 * @param method
 */
function request(url,param,succ,method) {
    token.getToken(function () {
        requestMethod(url,param,succ,method)
    })
}

/**
 * 分页请求
 * @param that
 * @param url
 * @param param
 * @param pos
 * @param fun
 */
function pageList(that,url,param,pos,fun='') {
    // checkLogin();
    token.getToken(function () {
        pageListRequest(that,url,param,pos,fun)
    })
}
/**
 * 上传图片
 * @param callback
 */
function uploadImage(callback,limit) {
    token.getToken(function () {
        uploadImageRequest(callback,limit)
    })
}


/**
 * 验证是否登录
 */
function checkLogin() {
    if(!app.globalData.openId){
        wx.redirectTo({
            url: '/pages/site/login/login',
        })
    }
}
/**
 * 通用请求方法实现
 * @param url
 * @param param
 * @param succ
 * @param method
 */
function requestMethod(url,param,succ,method){
    param.api_token = app.globalData.token;
    param.openid = app.globalData.openId;
    console.log(333333333);
    console.log(app.globalData.openId);
    // console.log( wx.getStorage('openId'));
    url = url.indexOf("http") ==-1?app.globalData.baseurl + url:url;
    console.log(url);
    wx.request({
        url:url,
        data: param,
        method: method||'POST',
        // header: {'content-type': 'application/json'},
        header: {'content-type': 'application/x-www-form-urlencoded'},
        success: function(res){
            console.log(res);
            res = res.data;
            if(res.code == 2){
                wx.redirectTo({
                    url: '/pages/site/login/login',
                })
            }
            if(typeof(succ) == "function"){
                succ(res)
            }
        },
        fail:function (res) {
            console.log(res)
            error(res.data.errMsg);
        },
        complete: function (e) {
            wx.hideNavigationBarLoading();
        }
    })
}



/**
 * 上传图片请求实现
 * @param callback
 */
function uploadImageRequest(callback,limit) {
    console.log(limit||9)
    wx.chooseImage({
        count:limit||9,
        success(res) {
            const tempFilePaths = res.tempFilePaths;
            console.log(tempFilePaths);
            for (var i = 0; i < tempFilePaths.length; i++) {
                wx.uploadFile({
                    url: app.globalData.baseurl + 'upload/file',
                    filePath: tempFilePaths[i],
                    name: 'file',
                    header: {'content-type': 'application/json'},
                    formData: {
                        api_token: app.globalData.token
                    },
                    success(res) {
                        res = res.data;
                        res = JSON.parse(res);
                        console.log(res);
                        if(typeof(callback) == "function"){
                            callback(res);
                        }
                    },
                    fail(res){
                        console.log(res);
                        error('出错了');
                    }
                })
            }

        }
    })
}


/**
 * 分页函数
 * @param that
 * @param url
 * @param param
 * @param pos
 * @param fun
 */
function pageListRequest(that,url,param,pos,fun='') {
    that.setData({
        tips:{
            no_data:false,
            show_loading:true,
        }
    });

    param.api_token = app.globalData.token;
    param.openid = app.globalData.openId;
    wx.request({
        url: app.globalData.baseurl + url,
        data: param,
        method: 'POST',
        header: {'content-type': 'application/x-www-form-urlencoded'},
        success: function (res) {
            res = res.data;
            console.log(res);
            if(res.code != 1 ){
                error('出错了',1500);
            }
            if(res.code == 1){
                if (0 == res.data.pages) {
                    that.setData({
                        'tips.no_data': true,
                        pageList:[],
                    });
                    if(typeof(fun) == "function"){
                        fun(res);
                    }
                    return ;
                }

                switch (pos) {
                    case "top":
                        console.log("top");

                        that.setData({
                            page: 1,
                            pages: res.data.pages,
                            pageList: res.data.pageList?res.data.pageList:[]
                        });
                        console.log(that.data.pageList);

                        // wx.hideNavigationBarLoading() //完成停止加载
                        // wx.stopPullDownRefresh() //停止下拉刷新
                        break;
                    case "bottom":
                        console.log("bottom");

                        that.setData({
                            pages: res.data.pages,
                            pageList: that.data.pageList.concat(res.data.pageList)
                        });
                        break;
                    default:
                        console.log("default");

                        that.setData({
                            page: 1,
                            pages: res.data.pages,
                            pageList: res.data.pageList?res.data.pageList:[]
                        });
                        break;
                }
                if (that.data.page == that.data.pages) {
                    that.setData({
                        'tips.no_data': true
                    });
                }

                if(typeof(fun) == "function"){
                    console.log('callback');
                    fun(res);
                }
            }
        },
        complete: function (e) {
            that.setData({
                'tips.show_loading': false,
            });
        }
    })
}


function error(title,delay=1500){
    wx.showToast({
        title: title||'出错了',
        icon: 'warn',
        image: '/images/warn.png',
        duration: delay
    })
}

function success(title,delay=1500){
    wx.showToast({
        title: title||'操作成功',
        icon: 'success',
        duration: delay
    })
}

/**
 * 顶部错误信息
 * @param msg
 * @param that
 */
function fail(msg,that){
    setTimeout(function(){
        that.setData(
            {
                popErrorMsg: msg
            }
        );
        ohShitfadeOut(that);
        return;
    },100)
}

function  ohShitfadeOut(that){
    var fadeOutTimeout = setTimeout(function () {
        that.setData({ popErrorMsg: '' });
        clearTimeout(fadeOutTimeout);
    }, 3000);
}



module.exports = {
  checkLogin:checkLogin,
  error: error,
  fail: fail,
  success:success,
  request: request,
  pageList:pageList,
  uploadImage:uploadImage,
  scene:'',
};
